//选值右移
/**
 * commonLang.tip.alert:提示
 * bigerr:实际输出字段个数不能大于应输出字段个数！
 * selectrightmove:请选择需要右移的字段！
 */
function oneAdd() {

    var row = columnsDataGrid.datagrid('getSelected');
    if (null != row) {

        getCurrColumnForCurrOutput();
        if (outputFields.length >= fieldCount) {
            $.messager.alert(commonLang.tip.alert, commonLang.tip.bigErr, 'warning');
            return;
        }
        var rows = colNamesDataGrid.datagrid('getRows');
        colNamesDataGrid.datagrid('appendRow', {
            TABLE_NAME: row.TABLE_NAME,
            TABLE_TITLE:row.TABLE_TITLE,
            COL_NAME:row.COL_NAME,
            COL_LABEL:row.COL_LABEL,
            FIELD_SORT: rows.length
        });
        getCurrColumnForCurrOutput();

    } else {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.selectRightMove, 'warning');
    }
}
//全右移
/**
 * commonLang.tip.alert:提示
 * bigerr:实际输出字段个数不能大于应输出字段个数！
 * noselectedmove:没有可移动的字段！
 */
function allAdd() {
    var row = columnsDataGrid.datagrid('getRows');
    if (row.length > 0) {

        if (row.length > fieldCount) {
            $.messager.alert(commonLang.tip.alert, commonLang.tip.bigErr, 'warning');
            return;
        }

        for ( var i = 0; i < row.length; i++) {
            colNamesDataGrid.datagrid('appendRow', row[i]);
            i--;
        }

        getCurrColumnForCurrOutput();

    } else {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.noSelectedMove, 'warning');
    }

}

//选值左移
/**
 * commonLang.tip.alert:提示
 *
 * selectleftmove:请选择需要左移的字段！
 */
function oneRemove() {
    var row = colNamesDataGrid.datagrid('getSelected');
    if (null != row) {
        var rowIndex = colNamesDataGrid.datagrid('getRowIndex', row);
        var leftRows = columnsDataGrid.datagrid('getRows');
        var isHave = false;
        for (var j = 0; j < leftRows.length; j++) {
            if ((row.TABLE_NAME + "." + row.COL_NAME) == (leftRows[j].TABLE_NAME + "." +leftRows[j].COL_NAME)) {
                isHave = true;
            }
        }
        if (isHave == false) {
            columnsDataGrid.datagrid('appendRow', row);
        }
        colNamesDataGrid.datagrid('deleteRow', rowIndex);
        getCurrColumnForCurrOutput();

    } else {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.selectLeftMove, 'warning');
    }
}

//全左移
/**
 * commonLang.tip.alert:提示
 *
 * noselectedmove:没有可移动的字段！
 */
function allRemove() {
    var row = colNamesDataGrid.datagrid('getRows');
    if (row.length > 0) {
        for ( var i = 0; i < row.length; i++) {
            var rowIndex = colNamesDataGrid.datagrid('getRowIndex', row[i]);
            var leftRows = columnsDataGrid.datagrid('getRows');
            var isHave = false;
            for (var j = 0; j < leftRows.length; j++) {
                if ((row[0].TABLE_NAME + "." + row[0].COL_NAME) == (leftRows[j].TABLE_NAME + "." +leftRows[j].COL_NAME)) {
                    isHave = true;
                }
            }
            if (isHave == false) {
                columnsDataGrid.datagrid('appendRow', row[i]);
            }
            colNamesDataGrid.datagrid('deleteRow', rowIndex);
            i--;
        }
        getCurrColumnForCurrOutput();
    } else {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.noSelectedMove, 'warning');
    }
}

//查重
function searchDatas() {
    var row = $("#colNamesDataGrid").datagrid('getRows');
    var cols = ""; //需要查重的字段
    for ( var i = 0; i < row.length; i++) {
        cols += row[i].colEnName + ",";
    }
    cols = cols.substring(0, cols.length - 1);
    parent.searchDatas(cols, tableId, tableName, dialogId, datagridId);
}

function rowColor(index, row) {

    if (row.colIsSys == "Y") {
        return 'color:blue;';
    }
}

function getCurrColumnForCurrOutput() {

    rows = colNamesDataGrid.datagrid('getRows');
    var text = "";
    outputFields = [];
    for ( var i = 0; i < rows.length; i++) {
        var colName = rows[i].COL_NAME;
        var o = {
            TABLE_NAME : rows[i].TABLE_NAME,
            COL_LABEL : rows[i].COL_LABEL,
            COL_NAME : colName
        };
        outputFields.push(o);

        if(colName == undefined || colName == null || colName == "" ){
            text = text + rows[i].COL_LABEL;
        }else{
            text = text + rows[i].TABLE_TITLE + "." + rows[i].COL_LABEL;
        }
        if (i != rows.length - 1) {
            text = text + ",";
        }

    }
    $("#currOutput").val(text);

}

//关闭
function cancle() {
    if (parent != null && parent.$('#' + dialogId) != null) {
        parent.$('#' + dialogId).dialog('close'); //关闭当前窗口
    }
}

// 保存数据源与模板信息
/**
 * bigerr:实际输出字段个数不能大于应输出字段个数
 * smallerr:实际输出字段个数不能小于应输出字段个数
 * remind:提示
 * savesuccess:保存成功
 * savefailure:保存失败
 *
 */
function save(bigerr,smallerr,remind,savesuccess,savefailure) {


    var outputFields = [];
    var tableNames = [];

    //循环未选中的字段行
    rows = columnsDataGrid.datagrid('getRows');
    for ( var i = 0; i < rows.length; i++) {
        var o = {
            TABLE_NAME : rows[i].TABLE_NAME,
            COL_LABEL : rows[i].COL_LABEL,
            COL_NAME : rows[i].COL_NAME
        };

        var tableName = rows[i].TABLE_NAME;
        if(tableName == null || tableName == "null" || tableName == ""){
            continue;
        }
        if (tableNames.length == 0) {
            tableNames.push(tableName);
        } else {
            var flag = false;
            for ( var j = 0; j < tableNames.length; j++) {
                var t = tableNames[j];
                if (t == tableName) {
                    flag = true;
                    break;
                }
            }

            if (flag) {
                // 当前tableName已经在tableNames中存在, 什么也不做

            } else {
                // 当前tableName在tableNames中不存在,需要将tableName加入到tableNames中
                tableNames.push(tableName);

            }
        }
    }
    //循环已选中的字段行
    rows = colNamesDataGrid.datagrid('getRows');
    for ( var i = 0; i < rows.length; i++) {
        var o = {
            TABLE_NAME : rows[i].TABLE_NAME,
            COL_LABEL : rows[i].COL_LABEL,
            COL_NAME : rows[i].COL_NAME
        };
        outputFields.push(o);

        var tableName = rows[i].TABLE_NAME;
        if(tableName == null || tableName == "null" || tableName == ""){
            continue;
        }
        if (tableNames.length == 0) {
            tableNames.push(tableName);
        } else {
            var flag = false;
            for ( var j = 0; j < tableNames.length; j++) {
                var t = tableNames[j];
                if (t == tableName) {
                    flag = true;
                    break;
                }
            }

            if (flag) {
                // 当前tableName已经在tableNames中存在, 什么也不做

            } else {
                // 当前tableName在tableNames中不存在,需要将tableName加入到tableNames中
                tableNames.push(tableName);

            }
        }
    }

    var errorMsg = "";

    if (outputFields.length > fieldCount) {
        errorMsg = commonLang.tip.bigErr;

    } else if (outputFields.length < fieldCount) {
        errorMsg = commonLang.tip.smallErr;

    }

    if ("" != errorMsg) {
        $.messager.alert(commonLang.tip.alert, errorMsg, 'warning');
        return;
    }

    var args = {
        sysId : commArgs.sysId,
        templateId : templateId, // 模板ID
        outputFields : fields, // 应输出字段
        currFields : outputFields, // 实际输出字段与表对应关系
        tableNames : tableNames
        // 可能会对应多个表
    };

    // 保存模板与数据源对应信息
    $.ajax({
        url : commArgs.url + '/save',
        dataType : 'json',
        type : 'post',
        data : {
            param : JSON.stringify(args)

        },
        success : function(result) {
            if (result.flag == "success") {

                sql = result.sql;
                parent.window.sql = sql;

                var dlg = $.messager.show({
                    title : commonLang.tip.alert,
                    msg : commonLang.optip.saveSuccess,
                    timeout : 0
                });
                // 使用该方法可以自定义一些关闭事件
                setTimeout(function() {
                    dlg.window('close');
                    // 刷新父页面的树
                    parent.reportManager.reLoadReportTreeRoot();
                    parent.reportManager.reLoadReportTree();
                    // 保存成功后，关闭页面
                    parent.$("#" + dialogId).dialog('close');

                }, 3000);

            } else {
                $.messager.show({
                    title : commonLang.tip.alert,
                    msg : commonLang.optip.saveFailure,
                    timeout : 4000,
                    showType : 'slide'
                });

            }
        }

    });

}